import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.ListIterator;
import javax.swing.*;

public class ViewImage extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	
	JButton jButton1, jButton2;
	JLabel jLabel1;
	static ListIterator<String> listIterator;
	String strImagePath="D://SamplePictures//Sunset.jpg";
	ImageIcon icon = new ImageIcon(strImagePath);
	
public ViewImage() {
		
		jButton1 = new JButton("Previous Image");
		jButton2 = new JButton("Next Image");
		
		jLabel1 = new JLabel(icon);
		jLabel1.setVerticalTextPosition(JLabel.BOTTOM);
		jLabel1.setHorizontalTextPosition(JLabel.CENTER);
		
		jButton1.addActionListener(this);
		jButton2.addActionListener(this);
		

		add(jButton1);
		add(jButton2);
		add(jLabel1);
		
		jLabel1.setSize(getMaximumSize());
		
	
	}
	
	public void actionPerformed(ActionEvent evt) {
	    Object src = evt.getSource();
	    if (src == jButton1) {
	    	
	    	if(listIterator.hasPrevious())
	    	{
	    		strImagePath=(String) listIterator.previous();
		    	icon.getImage().flush();
		    	icon=new ImageIcon(strImagePath);
		    	jLabel1.setIcon(icon);
		    	 		
	    	}
	    	else
	    	{
	    		JOptionPane.showMessageDialog(null,"First Image" );
	    	}
	    
	    	
	    	
	    } else if (src == jButton2) {
	    	
	    	if(listIterator.hasNext())
	    	{
	    		strImagePath=(String) listIterator.next();
		    	icon.getImage().flush();
		    	icon=new ImageIcon(strImagePath);
		    	jLabel1.setIcon(icon);
		    	
	    		}
	    	else
	    	{
	    		JOptionPane.showMessageDialog(null,"Last Image" );
		    	
	    	}
	    	
	    }
	    
	}
	    
	void imageSource(){
		 ArrayList<String> aList =new ArrayList<String>();
	
		 File ss1= new File(strImagePath);
		 String Parent= ss1.getParent();
		 

		  File f1 = new File(Parent);
	
		  
		  String[] childFiles = f1.list();
		  
		  
		  for (String s: childFiles){
			  
			  File srCc= new File(f1+File.separator+s);
			  
			  aList.add(srCc.getPath());
			 listIterator = aList.listIterator();
			  
		  }
	}
	
	public static void main(String[] args) {
		
		ViewImage vi=new ViewImage();
		vi.imageSource();
		JFrame frame = new JFrame("jLabel Usage Demo");
		
		frame.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		frame.setContentPane(new ViewImage());

		frame.setPreferredSize(new Dimension(900, 750));
		 
		frame.pack();
		frame.setVisible(true);
	}
}